﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace DataDownloader
{
    public partial class Form1 : Form
    {
        public LoginCredentials login;
        public CoreDump coreDownload;
        public Form1()
        {
            InitializeComponent();
            
        }

        public void SetProgressBar(int value)
        {
            progressBar.Value = value;
        }
        private void Set_Proxy_Click(object sender, EventArgs e)
        {
            try
            {
                login = new LoginCredentials(HttpProxy.Text, userName.Text, password.Text);                            
                
            }
            catch(Exception exp)
            {
            string str = exp.Message;
            }
       }

        private void DownloadSeries_Click(object sender, EventArgs e)
        {
            coreDownload = new CoreDump();
            ////var fileDownloadYear = Path.Combine(filePath.Text, year.Text);
            ////if (!Directory.Exists(fileDownloadYear))
            ////{
            ////    Directory.CreateDirectory(fileDownloadYear);
            ////}
            ////var fileDownloadSeries = Path.Combine(fileDownloadYear, team1.Text + "_" + team2.Text);
            ////coreDownload.DownloadTestSeries(url.Text, Convert.ToInt32(numTests.Text), login, fileDownloadSeries);
            coreDownload.Download_Series(login, url.Text, filePath.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            DownloadFile1(url.Text, login, filePath.Text);
            
        }

        public int DownloadFile1(string url, LoginCredentials login, string downloadPath)
        {
            try
            {
                WebClient client = new WebClient();
                client.Proxy = login.proxy;
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

                // Starts the download
                client.DownloadFileAsync(new Uri(url), downloadPath);

                button1.Text = "Download In Process";
                button1.Enabled = false;
                return 0;
            }
            catch (WebException webException)
            {
                webException.GetType();
                return -1;
            }

        }

        private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;
            if (percentage < 0)
            {
                return;
            }
            progressBar.Value = int.Parse(Math.Truncate(percentage).ToString());
        }

        private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show(" downloaded");
            progressBar.Value = 0;
            button1.Text = "Start Download";
            button1.Enabled = true;
            
        }




        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                HttpProxy.Text = "http://202.141.80.22:3128/";
            }
            else
            {
                HttpProxy.Text = "http://172.16.25.169:5431/";
            }
        }
                
    }
}
